package org.gangcai.mac.shop.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;

import org.greenrobot.eventbus.EventBus;
import org.gangcai.mac.shop.R;
import org.gangcai.mac.shop.bean.CommonResonseBean;
import org.gangcai.mac.shop.bean.EventBusBaseBean1;
import org.gangcai.mac.shop.bean.PullAuthoBean;
import org.gangcai.mac.shop.constants.Constant;
import org.gangcai.mac.shop.oberver.CommonObserver;
import org.gangcai.mac.shop.utils.MatisseUtils;

import java.io.File;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class AuthoTaobaoActivity extends BaseAuthoActivity {

    @BindView(R.id.address_img)
    ImageView addressImg;
    @BindView(R.id.order_img)
    ImageView orderImg;
    @BindView(R.id.nextStep)
    Button nextStep;
    private Uri addressImgUri;
    private Uri orderImgUri;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
        getAuthoData("gettaobao").subscribe(new CommonObserver<PullAuthoBean>() {
            @Override
            public void onSuccess(PullAuthoBean value) {
                PullAuthoBean.InfoBean info = value.getInfo();
                loadImage(orderImg, info.getOrder_img());
                loadImage(addressImg, info.getAddress_img());
            }
        });
    }

    @Override
    protected String getTitleName() {
        return "淘宝认证";
    }

    @Override
    public int getcontentView() {
        return R.layout.activity_autho_taobao;
    }

    @OnClick({R.id.address_img, R.id.order_img, R.id.nextStep})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.address_img:
                MatisseUtils.pickImage(this, 1, 100);
                break;
            case R.id.order_img:
                MatisseUtils.pickImage(this, 1, 200);
                break;
            case R.id.nextStep:
                if (addressImgUri == null) {
                    ToastUtils.showShort("请上传淘宝收货地址");
                } else if (orderImgUri == null) {
                    ToastUtils.showShort("请上传吧淘宝订单截图");
                } else {
                    submitData();
                }

                break;
        }
    }

    private void submitData() {
        prompDialog.showLoading("请等待");
        HashMap<String, String> paramMap = new HashMap<>();
        paramMap.put("uid", SPUtils.getInstance().getString("uid"));

        HashMap<String, File> fileMap = new HashMap<>();
        fileMap.put("address_img", MatisseUtils.getFile(addressImgUri, this));
        fileMap.put("order_img", MatisseUtils.getFile(orderImgUri, this));
        submitDataToServer("taobao_ident", paramMap, fileMap).subscribe(new CommonObserver<CommonResonseBean>(prompDialog) {
            @Override
            public void onSuccess(CommonResonseBean value) {
                if (value.getCode() == Constant.CODE_SUCC) {
                    showMsg();
                    EventBus.getDefault().postSticky(new EventBusBaseBean1());

                    finish();
                }
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == RESULT_OK && data != null) {

            List<Uri> images = MatisseUtils.getImages(data);
            if (requestCode == 100) {
                addressImgUri = images.get(0);
                Glide.with(this).load(addressImgUri).into(addressImg);
                orderImg.setVisibility(View.VISIBLE);
            } else if (requestCode == 200) {
                orderImgUri = images.get(0);
                Glide.with(this).load(orderImgUri).into(orderImg);
            }

        }
    }
}
